
def isPointInPolygon(polygon, p):
	OUTSIDE = False
	INSIDE = True
	counter = 0
	p1 = polygon[0]
	for i in range(1, len(polygon)):
		p2 = polygon[i%len(polygon)]
		if p[1] > min(p1[1], p2[1]):
			if p[1] <= max(p1[1], p2[1]):
				if p[0] <= max(p1[0], p2[0]):
					if p1[1] != p2[1]:
						xinters = (p[1]-p1[1])*(p2[0]-p1[0])/(p2[1]-p1[1])+p1[0]
						if p1[0] == p2[0] or p[0] <= xinters:
							counter = counter + 1
		p1 = p2	
	if (counter % 2) == 0:
		return OUTSIDE
	else:
		return INSIDE
		
		
##polygon = [[1,1], [3,1], [3,3], [1,3]]
##point = [0,1]

##if isPointInPolygon(polygon, point):
##	print 'Point is inside the polygon'
##else:
##	print 'Point is outside the polygon'
		